/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HAL_Button.h
* version:         1.0
* created_by:      Alejandro Ruiz
* date_created:    June 22 2013
*=============================================================================*/
/* DESCRIPTION : Source file for abstraction layer for BUTTON                 */
/*============================================================================*/
/* FUNCTION COMMENT : Source file abstraction layer for BUTTON                */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 22/jun/2013 |                               | Alejandro Ruiz   */
/* Initial version for Ale Window Lifter using Freescale Bolero micro.        */
/*============================================================================*/

#include "sysinit.h"
#include "HAL_Button.h"
#include "MAL_IOapi.h"


static T_UBYTE rub_Init10msUp = FALSE;
static T_UBYTE rub_Init10msDown = FALSE;
static T_UBYTE rub_Init10msAntipinch = FALSE;

static T_UBYTE rub_Init500msUp = FALSE;
static T_UBYTE rub_Init500msDown = FALSE;


void HAL_Button_Init(void)
{
	IO_SetPinAsInput(BUTTON_UP);     	/* Program the drive enable pin of S1  (PE0)  as input*/
	IO_SetPinAsInput(BUTTON_DOWN);     	/* Program the drive enable pin of S2  (PE1)  as input*/
	IO_SetPinAsInput(BUTTON_ANTIPINCH); /* Program the drive enable pin of S3  (PE2)  as input*/			
}


void HAL_Button_ReadButton(T_UBYTE lub_Channel, PTR_UBYTE lpub_Value)
{
	*lpub_Value = IO_PinRead(lub_Channel);
}


void HAL_Button_ValidButton(PTR_UBYTE lpub_Value)
{
	T_UBYTE lub_ButtonValue;
	T_ULONG lub_TempFlag;
		
	/*
        NO_ONE,
        INVALID,
        CONTINUOUS_UP,
		ONETOUCH_UP,
		CONTINUOUS_DOWN,
		ONETOUCH_DOWN,
		ANTIPINCH
	*/
	*lpub_Value = NO_ONE;
	
	HAL_Button_ReadButton(BUTTON_UP, &lub_ButtonValue);
	if(lub_ButtonValue == BUTTON_PRESSED)
	{
		if(rub_Init500msUp == FALSE)
		{			
			/* Validate button is pressed */
			if(rub_Init10msUp == FALSE)
			{
				MAL_Timer10ms_Init_channel();
				rub_Init10msUp = TRUE;
				
				rub_Init500msUp = FALSE;
			}
			else
			{
				lub_TempFlag = MAL_Read10msFLAG();
				if(lub_TempFlag == TRUE)
				{
					rub_Init10msUp = FALSE;
					
					*lpub_Value = ONETOUCH_UP;
					
					MAL_Timer500ms_Init_channel();
					rub_Init500msUp = TRUE;
				}
			}
		}
		else
		{
			lub_TempFlag = MAL_Read500msFLAG();
			if(lub_TempFlag == TRUE)
			{
				*lpub_Value = CONTINUOUS_UP;
			}
			else
			{
				*lpub_Value = ONETOUCH_UP;
			}
		}
		
	}
	else
	{
		if(rub_Init500msUp == TRUE)
		{
			*lpub_Value = ONETOUCH_UP;
			
			rub_Init500msUp = FALSE;
		}
	}
	
	HAL_Button_ReadButton(BUTTON_DOWN, &lub_ButtonValue);
	if(lub_ButtonValue == BUTTON_PRESSED)
	{
		if(rub_Init500msDown == FALSE)
		{			
			/* Validate button is pressed */
			if(rub_Init10msDown == FALSE)
			{
				MAL_Timer10ms_Init_channel();
				rub_Init10msDown = TRUE;
				
				rub_Init500msDown = FALSE;
			}
			else
			{
				lub_TempFlag = MAL_Read10msFLAG();
				if(lub_TempFlag == TRUE)
				{
					rub_Init10msDown = FALSE;
					
					if(*lpub_Value == CONTINUOUS_UP)
					{
						*lpub_Value = INVALID;
					}
					else if(*lpub_Value == ONETOUCH_UP)
					{
						*lpub_Value = NO_ONE;
					}
					else
					{
						*lpub_Value = ONETOUCH_DOWN;	
					}					
					
					MAL_Timer500ms_Init_channel();
					rub_Init500msDown = TRUE;
				}
			}
		}
		else
		{
			lub_TempFlag = MAL_Read500msFLAG();
			if(lub_TempFlag == TRUE)
			{
				
				if(*lpub_Value == CONTINUOUS_UP)
				{
					*lpub_Value = INVALID;
				}
				else if(*lpub_Value == ONETOUCH_UP)
				{
					*lpub_Value = NO_ONE;
				}
				else
				{
					*lpub_Value = CONTINUOUS_DOWN;	
				}
				
			}
			else
			{
				if(*lpub_Value == CONTINUOUS_UP)
				{
					*lpub_Value = INVALID;
				}
				else if(*lpub_Value == ONETOUCH_UP)
				{
					*lpub_Value = NO_ONE;
				}
				else
				{
					*lpub_Value = ONETOUCH_DOWN;	
				}	
			}
		}
		
	}
	else
	{
		if(rub_Init500msDown == TRUE)
		{
			if(*lpub_Value == CONTINUOUS_UP)
			{
				*lpub_Value = INVALID;
			}
			else if(*lpub_Value == ONETOUCH_UP)
			{
				*lpub_Value = NO_ONE;
			}
			else
			{
				*lpub_Value = ONETOUCH_DOWN;	
			}	
			
			rub_Init500msDown = FALSE;
		}
	}

	HAL_Button_ReadButton(BUTTON_ANTIPINCH, &lub_ButtonValue);
	if(lub_ButtonValue == BUTTON_PRESSED)
	{					
		/* Validate button is pressed */
		if(rub_Init10msAntipinch == FALSE)
		{
			MAL_Timer10ms_Init_channel();
			rub_Init10msAntipinch = TRUE;
		}
		else
		{
			lub_TempFlag = MAL_Read10msFLAG();
			if(lub_TempFlag == TRUE)
			{
				rub_Init10msAntipinch = FALSE;
				
				if((*lpub_Value != CONTINUOUS_DOWN) || (*lpub_Value != ONETOUCH_DOWN))
				{
					*lpub_Value = ANTIPINCH;
				}
				/*if((*lpub_Value == CONTINUOUS_UP) || (*lpub_Value == ONETOUCH_UP))
				{
					*lpub_Value = ANTIPINCH;
				}*/
				
				
			}
		}	
				
	}

	
}

